package org.qydjk.console.controller.specialist;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.specialist.Specialist;
import org.qydjk.common.shiro.vo.ShiroUser;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.common.vo.autocomplete.AutoCompleteBean;
import org.qydjk.common.vo.autocomplete.AutoCompleteResponse;
import org.qydjk.console.vo.SpecialistExtendBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 专家Controller
 * Created by Edward_yang on 2018/1/8 0008.
 */
@RequestMapping("/specialist")
@Controller
public class SpecialistController extends BaseController {
    /***
     * 前往专家list
     * @author Edward_yang
     * @uptime 2017-01-08
     * @return
     */
    @RequestMapping("/gotoSpecialist.do")
    public String gotoSpecialist() {
        return "/specialist/speciaListInfo";
    }

    @RequestMapping("/specialist.do")
    @ResponseBody
    public Object specialist(Integer page, Integer rows,String phone, String specialistName, String verifyType, String status){
        HibernatePage pager = new HibernatePage(page, rows);
        String sql = "SELECT  qys.id as id,qys.name as name,qys.profession_position as professionPosition,qys.tel as tel,qys.img as img, qys.verify_type as verifyType , qys.status as status,qys.virtaul_comment_count as virtaulCommentCount , qys.comment_count as commentCount ,qys.comment_score as commentScore,qys.ord as ord,qys.create_time as createDate, qhd.id as hospitalDepartmentId , qhd.name as hospitalDepartmentName, qs.id as sellerId ,qs.name as sellerName,qu.id as userId ,qu.phone as userPhone " +
                "  From qy_seller qs, qy_user qu ,qy_specialist qys , qy_hospital_department qhd " +
                "  where qs.id=qys.seller_id AND  qu.id=qys.user_id AND qhd.id =qys.hospital_department_id";
        String sqlCount = "SELECT count(qys.id) " +
                "From qy_seller qs, qy_user qu ,qy_specialist qys , qy_hospital_department qhd  " +
                "  where qs.id=qys.seller_id AND  qu.id=qys.user_id AND qhd.id =qys.hospital_department_id";
        if (!StringUtil.isEmptyOrNull(phone)) {
            sql += " and qu.phone=" + phone;
            sqlCount += " and qu.phone=" + phone;
        }
        if (!StringUtil.isEmptyOrNull(specialistName)) {

            sql += " and qys.name like'%" + specialistName + "%'";
            sqlCount += " and qys.name like'%" + specialistName + "%'";
        }
        if (!StringUtil.isEmptyOrNull(verifyType)) {
            if (!"-1".equals(verifyType)) {
                sql += " and qs.verify_type=" + verifyType;
                sqlCount += " and qs.verify_type=" + verifyType;
            }
        }
        if (!StringUtil.isEmptyOrNull(status)) {
            if (!"-1".equals(status)) {
                sql += " and qs.status=" + status;
                sqlCount += " and qs.status=" + status;
            }
        }
        sql += " ORDER BY  qys.ord desc";
        try {
            List<Map<String, Object>> specialist = baseService.findMapBySQL(sql, pager);
            int total = baseService.getRecordCountSQL(sqlCount);

            return new EasyUIResponse(total, specialist);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /***
     * 添加页面
     * @author Edward_yang
     * @uptime 2017-01-08
     * @return
     */
    @RequestMapping("/gotoAddSpecialist.do")
    public String gotoAddSpecialist() {
        return "specialist/specialist_add";
    }

    /***
     * 专家添加
     * @author Edward_yang
     * @uptime 2017-01-08
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Object addSpecialist(SpecialistExtendBean specialistExtendBean) {
        ShiroUser shiroUser = getShiroUser();
        if (specialistExtendBean.getUserId() == null) {
            return renderError("请填写注册的用户账号");
        }
        try {
            Specialist specialist = baseService.get(Specialist.class, specialistExtendBean.getUserId());
            if(specialist!=null){
                return renderError("用户账号已经注册");
            }
            Specialist specialistXIN=new Specialist();
            BeanUtil.copyProperties(specialistExtendBean,specialistXIN);
            specialistXIN.setStatus(1);
            specialistXIN.setVerifyType(0);
            specialistXIN.setUpdateTime(new Date());
            specialistXIN.setCreateTime(new Date());
             baseService.save(specialistXIN);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /***
     * 编辑页面
     * @author Edward_yang
     * @uptime 2017-12-20
     * @return
     */
    @RequestMapping("/gotoEditSpecialist.do")
    public String gotoEditSpecialist(Integer specialistId) {
        try {
            Specialist specialist = baseService.get(Specialist.class, specialistId);
            String sql = "SELECT   qhd.name as hospitalDepartmentName, qs.name as sellerName,qu.phone as userPhone " +
                    "  From qy_seller qs, qy_user qu ,qy_specialist qys , qy_hospital_department qhd " +
                    "  where qs.id=qys.seller_id AND  qu.id=qys.user_id AND qhd.id =qys.hospital_department_id AND qys.id="+specialistId;
            List<Map<String, Object>> specialistMap = baseService.findMapBySQL(sql);
            request.setAttribute("specialistMap",specialistMap.get(0));
            request.setAttribute("specialist", specialist);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "specialist/specialist_edit";
    }

    /***
     * 商家修改
     * @author Edward_yang
     * @uptime 2017-12-20
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object editSpecialist(SpecialistExtendBean specialistExtendBean) {
        ShiroUser shiroUser = getShiroUser();
        if (specialistExtendBean.getUserId() == null) {
            return renderError("请填写注册的用户账号");
        }
        try {
            Specialist specialist = baseService.get(Specialist.class, specialistExtendBean.getId());
            BeanUtil.copyProperties(specialistExtendBean,specialist);
            specialist.setUpdateTime(new Date());
            baseService.saveOrUpdate(specialist);
                return renderSuccess();

        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }

    }
    /***
     * 专家删除
     * @author Edward_yang
     ** @uptime 2017-01-08
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object deleteSpecialist(Integer specialistId) {
        ShiroUser shiroUser = getShiroUser();
        try {
            Specialist specialist = baseService.get(Specialist.class, specialistId);
            baseService.delete(specialist);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /****
     * 跳转专家审核
     * @author Edward_yang
     * @uptime 2017-01-08
     * @return
     */
    @RequestMapping("/gotoCheckSpecialist.do")
    public String gotoCheckSpecialist(Integer specialistId) {
        try {
            Specialist specialist = baseService.get(Specialist.class, specialistId);
            request.setAttribute("specialist", specialist);
            specialist.setUpdateTime(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "specialist/specialist_checkSpecialist";
    }
    /***
     *专家审核
     * @author Edward_yang
     * @uptime 2017-01-08
     */
    @RequestMapping("/checkSpecialist.do")
    @ResponseBody
    public Object checkSpecialist(Integer specialistId, Integer specialistType) {
        ShiroUser shiroUser = getShiroUser();
        try {
            Specialist specialist = baseService.get(Specialist.class, specialistId);
            specialist.setVerifyType(specialistType);
            specialist.setUpdateTime(new Date());
            baseService.saveOrUpdate(specialist);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /****
     * 跳转商家启用和禁用
     * @author Edward_yang
     * @uptime 2017-12-18
     * @return
     */
    @RequestMapping("/gotoCheckStauteSpecialist.do")
    public String gotoCheckStauteSpecialist(Integer specialistId) {
        try {
            Specialist specialist = baseService.get(Specialist.class, specialistId);
            request.setAttribute("specialist", specialist);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "specialist/specialist_checkStauteSpecialist";
    }

    /***
     *商家启用和禁用
     * @author Edward_yang
     * @uptime 2017-12-15
     */
    @RequestMapping("/checkStauteSpecialist.do")
    @ResponseBody
    public Object checkStauteSpecialist(Integer specialistId, Integer status) {

        try {
            Specialist specialist = baseService.get(Specialist.class, specialistId);
            specialist.setStatus(status);
            specialist.setUpdateTime(new Date());
            baseService.saveOrUpdate(specialist);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /**
     * 自动补全 用户信息
     *
     * @author Edward_yang
     * @uptime 2017-12-20
     */
    @RequestMapping("/autoCompleteUserData.do")
    @ResponseBody
    public Object  autoCompleteUserData() {
        List<AutoCompleteBean> list = new ArrayList<AutoCompleteBean>();
        String query = request.getParameter("query");
        if (StringUtil.isEmptyOrNull(query)) {
            return new AutoCompleteResponse(list);
        }
        String sql = "SELECT concat(phone) AS `value`,concat(id,'') AS `data` FROM qy_user WHERE   type=3 and phone LIKE '%" + query + "%'";
        try {
            list = baseService.findBeanBySQL(sql, AutoCompleteBean.class, new HibernatePage(1, 10));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AutoCompleteResponse(list);
    }
    /**
     * 自动补全 所属医院
     *
     * @author Edward_yang
     * @uptime 2017-12-20
     */
    @RequestMapping("/autoCompleteSellerData.do")
    @ResponseBody
    public Object  autoCompleteSellerData() {
        List<AutoCompleteBean> list = new ArrayList<AutoCompleteBean>();
        String query = request.getParameter("query");
        if (StringUtil.isEmptyOrNull(query)) {
            return new AutoCompleteResponse(list);
        }
        String sql = "SELECT concat(name) AS `value`,concat(id,'') AS `data` FROM qy_seller WHERE  name LIKE '%" + query + "%'";
        try {
            list = baseService.findBeanBySQL(sql, AutoCompleteBean.class, new HibernatePage(1, 10));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AutoCompleteResponse(list);
    }
    /**
     * 自动补全 所属医院
     *
     * @author Edward_yang
     * @uptime 2017-12-20
     */
    @RequestMapping("/autoCompleteHospitalDepartmentData.do")
    @ResponseBody
    public Object  autoCompleteHospitalDepartmentData() {
        List<AutoCompleteBean> list = new ArrayList<AutoCompleteBean>();
        String query = request.getParameter("query");
        if (StringUtil.isEmptyOrNull(query)) {
            return new AutoCompleteResponse(list);
        }
        String sql = "SELECT concat(name) AS `value`,concat(id,'') AS `data` FROM qy_hospital_department WHERE name LIKE '%" + query + "%'";
        try {
            list = baseService.findBeanBySQL(sql, AutoCompleteBean.class, new HibernatePage(1, 10));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AutoCompleteResponse(list);
    }
}
